package com.zc.im.service.user.impl;

import com.zc.im.mapper.user.ZcUserMapper;
import com.zc.im.model.user.ZcUser;
import com.zc.im.model.user.ZcUserExample;
import com.zc.im.model.user.ZcUserKey;
import com.zc.im.service.user.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServicelmpl implements UserService {

    @Autowired
    private ZcUserMapper zcUserMapper;

    private static Logger logger = Logger.getLogger(UserServicelmpl.class);

    @Override
    public ZcUser queryUser(Integer uid) {
        ZcUser zcUser = null;
        if (uid == null) {
            logger.info("userID is null ");
            return zcUser;
        }
        try {
            ZcUserExample userExample = new ZcUserExample();
            ZcUserExample.Criteria criteria = userExample.createCriteria();
            criteria.andUidEqualTo(uid);

            List<ZcUser> zcUsers = zcUserMapper.selectByExample(userExample);
            if(zcUsers == null || zcUsers.size() == 0){
                return zcUser;
            }
            zcUser = zcUsers.get(0);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return zcUser;
    }

    @Override
    public Integer updateUser(ZcUser zcUserModel) {
        Integer updateUserId = null;
        if (zcUserModel == null) {
            logger.info("updateUser add_time is null");
            return updateUserId;
        }
        try {
            ZcUserExample userExample = new ZcUserExample();
            ZcUserExample.Criteria criteria = userExample.createCriteria();
            criteria.andUidEqualTo(zcUserModel.getUid());
            updateUserId = zcUserMapper.updateByExample(zcUserModel, userExample);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return updateUserId;
    }


    public ZcUser findUser(Integer userId){
        ZcUser zcUser = null;
        if(userId == null || userId == 0){
            return zcUser;
        }
        ZcUserKey key = new ZcUserKey();
        key.setUid(userId);
        zcUser = zcUserMapper.selectByPrimaryKey(key);
        return zcUser;
    }


    /**
     * 查询用户列表
     * 2017-7-15 20:10:13
     * @param userIds
     * @return
     */
    public List<ZcUser> queryUsers(List<Integer> userIds){
        List<ZcUser> zcUsers = new ArrayList<>();
        if(userIds == null || userIds.size() == 0){
            return zcUsers;
        }
        ZcUserExample userExample = new ZcUserExample();
        ZcUserExample.Criteria criteria =  userExample.createCriteria();

        criteria.andUidIn(userIds);
        zcUsers = zcUserMapper.selectByExample(userExample);
        for(ZcUser user: zcUsers){
            user.setPass(null);
        }
        return zcUsers;
    }
}
